/*Copyright (C) 2009 Careil Baptiste

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <errno.h>
#ifdef WIN32
# include <winsock2.h>
# undef ERROR
#endif /* WIN32 */
#include "Network.h"

CSLP_NETWORK_ERROR DLL_IMPEXP CSLP_GetLastNetworkError()
{
  CSLP_NETWORK_ERROR retVal;
#ifdef WIN32
  switch (WSAGetLastError())
    {
    case WSAECONNREFUSED:
      retVal = DISCONNECTED;
      break;
    case WSAESHUTDOWN:
      retVal = DISCONNECTED;
      break;
    case WSAENETUNREACH:
      retVal = DISCONNECTED;
      break;
    case WSAENETRESET:
      retVal = DISCONNECTED;
      break;
    case WSAECONNABORTED:
      retVal = DISCONNECTED;
      break;
    case WSAECONNRESET:
      retVal = DISCONNECTED;
      break;
    case WSAETIMEDOUT:
      retVal = DISCONNECTED;
      break;
    case WSAEHOSTDOWN:
      retVal = DISCONNECTED;
      break;
    case WSAEHOSTUNREACH:
      retVal = DISCONNECTED;
      break;
    default:
      retVal = ERROR;
      break;
    }
#else /* WIN32 */
  switch(errno)
    {
    case ECONNREFUSED:
      retVal = DISCONNECTED;
      break;
    case EPIPE:
      retVal = DISCONNECTED;
      break;
    case ENETUNREACH:
      retVal = DISCONNECTED;
      break;
    case ENETRESET:
      retVal = DISCONNECTED;
      break;
    case ECONNABORTED:
      retVal = DISCONNECTED;
      break;
    case ECONNRESET:
      retVal = DISCONNECTED;
      break;
    case ETIMEDOUT:
      retVal = DISCONNECTED;
      break;
    case EHOSTDOWN:
      retVal = DISCONNECTED;
      break;
    case EHOSTUNREACH:
      retVal = DISCONNECTED;
      break;
    default:
      retVal = ERROR;
      break;
    }
#endif /* !WIN32 */
  return retVal;
}
